<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form #testScriptContainer class="cf-test-dialog-container size-full" [formGroup]="calculatedFieldScriptTestFormGroup">
  <mat-toolbar class="flex justify-between" color="primary">
    <h2>{{ 'calculated-fields.test-script-function' | translate }} ({{ 'api-usage.tbel' | translate }})</h2>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content class="relative">
    <div class="tb-absolute-fill">
      <div class="tb-fullscreen-panel flex size-full flex-row lt-md:flex-col">
        <div #leftPanel class="test-block-content overflow-hidden">
          <div class="relative size-full min-w-64">
            <div class="block-label-container left">
              <span class="block-label">{{ 'calculated-fields.expression' | translate }}</span>
            </div>
            <tb-js-func
              #expressionContent
              formControlName="expression"
              functionName="calculate"
              class="expression-edit"
              [functionArgs]="functionArgs"
              [required]="true"
              [disableUndefinedCheck]="true"
              [fillHeight]="true"
              [highlightRules]="data.argumentsHighlightRules"
              [scriptLanguage]="ScriptLanguage.TBEL"
              [editorCompleter]="data.argumentsEditorCompleter"
              [helpPopupStyle]="{ width: '1200px' }"
              resultType="object"
              helpId="calculated-field/expression_fn"
            />
          </div>
        </div>
        <div #rightPanel>
          <div #topRightPanel class="test-block-content">
            <div class="relative flex size-full min-w-80 gap-2">
              <div class="block-label-container right-top">
                <span class="block-label">{{ 'calculated-fields.arguments' | translate }}</span>
              </div>
              <tb-calculated-field-test-arguments class="size-full" formControlName="arguments" [argumentsTypeMap]="argumentsTypeMap"/>
            </div>
          </div>
          <div #bottomRightPanel class="test-block-content">
            <div class="relative size-full">
              <div class="block-label-container right-bottom">
                <span class="block-label" translate>common.output</span>
              </div>
              <tb-json-content
                class="flex-1"
                formControlName="output"
                label="{{ 'common.output' | translate }}"
                [contentType]="ContentType.JSON"
                validateContent="false"
                readonly="true"
                [fillHeight]="true"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div mat-dialog-actions class="flex flex-row">
    <button mat-button mat-raised-button color="primary"
            type="button"
            (click)="onTestScript()"
            [disabled]="(isLoading$ | async) || calculatedFieldScriptTestFormGroup.invalid">
      {{ 'action.test' | translate }}
    </button>
    <span class="flex-1"></span>
    <button mat-button color="primary"
            type="button"
            cdkFocusInitial
            [disabled]="(isLoading$ | async)"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-button mat-raised-button color="primary"
            type="submit"
            (click)="save()"
            [disabled]="(isLoading$ | async) || calculatedFieldScriptTestFormGroup.get('expression').invalid || !calculatedFieldScriptTestFormGroup.get('expression').dirty">
      {{ 'action.save' | translate }}
    </button>
  </div>
</form>
